<?php

namespace App\Listener;

use App\Event\ConsumeEvent;
use App\Model\CloseFriend;
use App\Model\CloseFriendLog;
use App\Model\Message;
use App\Model\User;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 亲密度监听
 * @Listener
 */
class CloseValueListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            ConsumeEvent::class,
        ];
    }

    public function process(object $event)
    {
        $closeValue = $event->costCoin * 0.01;
        $userId = $event->userId;
        $hostId = $event->hostUserId;
        $result = CloseFriend::addClose($userId, $hostId, $closeValue, $event->costCoin);
        //新增记录
        CloseFriendLog::addRecord($userId, $hostId, $closeValue);
        log_info(__CLASS__ . "==Uid:{$userId}==HostId:{$hostId}==Coin:{$event->costCoin}==Close:{$closeValue}");

        //亲密值消息提醒
        $crossLevel = CloseFriend::checkCrossLevel($userId, $hostId, $closeValue);
        if ($crossLevel['result'] && $crossLevel['level'] > 0) {
            $info = [
                'level' => $crossLevel['level'],
                'level_title' => $crossLevel['level_title'],
                'unlock_content' => $crossLevel['unlock_content'],
                'close_value' => $crossLevel['unlock_close_value'],
            ];
            $versions = User::checkVersion([$userId, $hostId]);
            //亲密值
            if ($versions[$userId] >= '1.0.0' && $versions[$hostId] >= '1.0.0') {
                Message::sendCloseTip($userId, $hostId, $info);
            }

            //主播视频引导
            if ($versions[$hostId] >= '1.0.0') {
                Message::sendSlient($hostId, 'HOST_CALL_VIDEO_TIP', [
                    'title' => '视频通话',
                    'text' => '恭喜你们亲密值达到0.1度，解锁了拨打视频/语音通话功能，快跟他视频聊聊吧～
视频邀请男方同意后需要根据你设定的通话费用进行付费哦～',
                    'button_text' => '马上邀请他视频通话～',
                    'user_id' => $userId,
                ]);
            }
        }
    }
}
